package from_0_to_50;

import com.sun.org.apache.regexp.internal.RE;

public class Q014 {
    public static void main(String[] args) {

        String[] strs = new String[]{"ab", "ab"};
        System.out.println("longestCommonPrefix(strs) = " + longestCommonPrefix(strs));

    }

    public static String longestCommonPrefix(String[] strs) {
        StringBuffer SB = new StringBuffer();
        if (strs.length == 1) {
            SB.append(strs[0]);
        } else {
            String minLenString = "";
            int j = 300;
            for (int i = 0; i < strs.length; i++) {
                if(strs[i].length() == 0){
                    return "";
                }
                if(strs[i].length() <= j){
                    j = strs[i].length();
                    minLenString = strs[i];
                }
            }
            System.out.println("minLenString = " + minLenString);
            for (int i = 0; i < minLenString.length(); i++) {
                char t = minLenString.charAt(i);
                for (j = 0; j < strs.length; j++) {
                    if(t != strs[j].charAt(i)){
                        return SB.toString();
                    }
                }
                System.out.println("j = " + j);
                System.out.println("strs.length = " + strs.length);
                if(j == strs.length)
                    SB.append(t);
                System.out.println("SB.toString() = " + SB.toString());
            }
        }
        return SB.toString();

    }
}
